package com.customization.test.demo.workflowaction;

import com.customization.unionpay.market.util.FormDataUtils;
import com.customization.unionpay.market.util.PropUtils;
import weaver.conn.RecordSetTrans;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.soa.workflow.request.RequestInfo;

import java.util.List;
import java.util.Map;
import weaver.interfaces.workflow.action.Action;
import weaver.workflow.request.OverTimeSetBean;
import weaver.workflow.request.RequestManager;

/**
 * @description 自定义流程事件
 * @create 2024-04-21 11:37
 */
public class MainAction implements Action{

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private String testIndex;

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestId = requestInfo.getRequestid();     // 流程id
        int testId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "test_id"));
        logger.info("Action-start..." +
                "requestid:" + requestId+",testIndex:"+testIndex);
        OverTimeSetBean overTimeBean = new OverTimeSetBean();

        RequestManager rm = requestInfo.getRequestManager();

        try {

            String src = rm.getSrc();

            int formId = rm.getFormid();                       // 流程表单id
            int isBill = rm.getIsbill();                       // 是否为单据
            int user = rm.getUser().getUID();                  // 当前操作者
            String tableName = rm.getBillTableName();          // 表单名称

            String requestName = requestInfo.getDescription(); // 流程标题
            String creatorId = requestInfo.getCreatorid();     // 流程创建人ID

            int nodeid = rm.getNodeid();
            if(src.equals("reject")){
                // 延时判断当前节点是否回退到发起节点
                String nodename = overTimeBean.getNodeName(nodeid);
                Map<String, Object> allFormData = FormDataUtils.getAllFormData(requestInfo);
            }else {
                String nodename = overTimeBean.getNodeName(nodeid);
                Map<String, Object> allFormData = FormDataUtils.getAllFormData(requestInfo);
                Map<String, String>  mainMap = (Map<String, String>)allFormData.get("mainMap");
                TestMoreAction testMoreAction = new TestMoreAction();
                // 所有明细表的值  sqr bm gs
                List<String> paramsTest = testMoreAction.getParamsTest(mainMap);

                Map<String,Map <String, Map<String, String>>>  detailMap = (Map<String, Map<String, Map<String, String>>>) allFormData.get("detailMap");

                Map<String, Map<String, String>> dt1 = detailMap.get("dt1");

                for (String key : dt1.keySet()) {
                    Map<String, String> value = dt1.get(key);

                }

                if("测试1".equals(nodename)){

                    String selectValueMan = FormDataUtils.getItemValue(formId, "xb", "男");
                    String xb = mainMap.get("xb");
                    if(xb.equals(selectValueMan)){
                        RecordSetTrans rst = new RecordSetTrans();
                        // 开启事务（关闭自动提交）
                        rst.setAutoCommit(false);
                        try {
                            String updateSql = "update #{tableName} set js=?  where requestId = ? ";
                            updateSql = updateSql.replace("#{tableName}",tableName);
                            rst.executeUpdate(updateSql,"错误1111",requestId);
                            // 提交事务
                            rst.commit();
                            FormDataUtils.rejectRequest(Integer.parseInt(requestId),user);
                        } catch (Exception e) {
                            // 回滚
                            rst.rollback();
                        }
                    }
                }

//                if (StrUtil.isNotBlank(selectValueMan)) {
//                    WorkflowToDocUtils.createHtml(requestInfo,"F:\\test\\","test");
//                }

            }

        }catch (Exception e){
            //异常报错是填写异常信息，阻止流程继续流转
            requestInfo.getRequestManager().setMessageid("10001");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());

            return Action.FAILURE_AND_CONTINUE;
        }

        return Action.SUCCESS;
    }

}
